package com.taokatao.cardmee.util;

import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taokatao.cardmee.BusEvent;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;


public class Once {

    public static void coverMainCard(ViewGroup parent, int currCardCount) {
        if (currCardCount > 0) {
            if (!coverCardScan(parent)) {
                if (currCardCount > 5) {
                    if (coverCardSortDrag(parent)) {
                        Utils.postMainThreadBusEvent(BusEvent.CARD_LIST_RETURN_TO_TOP);
                    }
                }
            }
        }
    }

    private static boolean coverCardScan(final ViewGroup parent) {
        if (parent == null) return false;
        Object fitFlag = parent.getTag(R.id.image_carry);
        if (fitFlag != null && TextUtils.equals((String) fitFlag, "fit_scan")) return true;

        boolean hasShown = Utils.spReadBoolean("cover_card_scan", false);
        if (!hasShown) {
            final View cover = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_once_card_scan, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                View vFit = cover.findViewById(R.id.v_fit);
                final int statusBarHeight = Utils.calcStatusBarHeight(parent.getContext());
                vFit.getLayoutParams().height += statusBarHeight;
            }

            cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    parent.setTag(R.id.image_carry, "");
                    Utils.spWrite("cover_card_scan", true);
                    cover.setVisibility(View.GONE);
                }
            });
            parent.addView(cover);
            parent.setTag(R.id.image_carry, "fit_scan");
            return true;
        }
        return false;
    }

    private static boolean coverCardSortDrag(final ViewGroup parent) {
        if (parent == null) return false;
        Object fitFlag = parent.getTag(R.id.image_carry_2);
        if (fitFlag != null && TextUtils.equals((String) fitFlag, "fit_sort")) return true;

        boolean hasShown = Utils.spReadBoolean("cover_card_sort", false);

        if (!hasShown) {
            final View cover = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_once_card_sort_drag, null);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                View vFit = cover.findViewById(R.id.v_fit);
                final int statusBarHeight = Utils.calcStatusBarHeight(parent.getContext());
                vFit.getLayoutParams().height += statusBarHeight;
            }

            cover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    parent.setTag(R.id.image_carry_2, "");
                    Utils.spWrite("cover_card_sort", true);
                    cover.setVisibility(View.GONE);
                }
            });
            parent.addView(cover);
            parent.setTag(R.id.image_carry_2, "fit_sort");
            return true;
        }
        return false;
    }

    @SuppressWarnings("unused")
    public void clearHistory() {
    }
}
